package com.ebo.learn_demo

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import androidx.work.*
import com.bumptech.glide.Glide
import com.ebo.learn_demo.databinding.ItemSwipeCardBinding
import com.ebo.learn_demo.leo_slidecard.CardConfig
import com.ebo.learn_demo.leo_slidecard.SlideCallback
import com.ebo.learn_demo.leo_slidecard.SlideCardBean
import com.ebo.learn_demo.view.MySlideLayoutManager
import com.enabot.lib.base.adapter.BaseRecycleViewAdapter
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.util.concurrent.TimeUnit

class SlidePageActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.e("TEST", "SlidePageActivity onCreate: ")
        setContentView(R.layout.activity_main2)
        val rv: RecyclerView = findViewById(R.id.rv)
        val bt: Button = findViewById(R.id.bt)
        bt.setOnClickListener {
            val myWorkRequest = OneTimeWorkRequestBuilder<UploadWork>()
                .setInitialDelay(2, TimeUnit.SECONDS)
                .build()
            WorkManager.getInstance(applicationContext).beginUniqueWork(
                "UploadWork",
                ExistingWorkPolicy.REPLACE,
                myWorkRequest
            ).enqueue()
        }
        rv.layoutManager = MySlideLayoutManager()
        val mDatas = SlideCardBean.initDatas()
        val adapter =
            object : BaseRecycleViewAdapter<ItemSwipeCardBinding, SlideCardBean>(this, mDatas) {
                override fun createItemView(
                    layoutInflater: LayoutInflater,
                    parent: ViewGroup,
                    viewType: Int
                ): ItemSwipeCardBinding {
                    return ItemSwipeCardBinding.inflate(layoutInflater, parent, false)
                }

                @SuppressLint("SetTextI18n")
                override fun bindItemView(itemView: ItemSwipeCardBinding, position: Int) {
                    val slideCardBean = mList[position]
                    itemView.tvName.text = slideCardBean.name
                    itemView.tvPrecent.text = slideCardBean.postition.toString() + "/" + mDatas.size
                    Glide.with(this@SlidePageActivity)
                        .asDrawable()
                        .load(slideCardBean.url)
                        .error(R.mipmap.ic_launcher)
                        .into(itemView.iv)
                }
            }

        rv.adapter = adapter
        // 初始化数据
        CardConfig.initConfig(this)
        val slideCallback = SlideCallback(adapter, mDatas)
        val itemTouchHelper = ItemTouchHelper(slideCallback)
        itemTouchHelper.attachToRecyclerView(rv)
    }

    class UploadWork(var app: Context, workerParams: WorkerParameters) : Worker(app, workerParams) {

        override fun doWork(): Result {
            Log.e("TEST", "doWork")
            GlobalScope.launch {
                delay(1000)
                Log.e("TEST", "doWork killProcess")
                val launchIntentForPackage = Intent(app, SlidePageActivity::class.java)
                launchIntentForPackage.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                app.startActivity(launchIntentForPackage)
                android.os.Process.killProcess(android.os.Process.myPid())
            }
            return Result.success().apply {
                Log.e("TEST", "doWork success")
            }
        }
    }
}